package com.nf.health.app.widget.custom_chatrow;

import android.content.Context;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.chat.EMTextMessageBody;
import com.hyphenate.easeui.widget.CircularImage;
import com.hyphenate.easeui.widget.chatrow.EaseChatRow;
import com.hyphenate.exceptions.HyphenateException;
import com.nf.health.app.R;

import org.json.JSONException;
import org.json.JSONObject;


/**
 * Created by 恩福 on 2016/10/10.
 * 系统消息 去报名 Item
 * @auther changpeng
 * @email obj_cp@126.com
 */

public class EaseChatRowSystemCard extends EaseChatRow {

    private TextView tvTitle;
    private TextView tvSubTitle;
    private CircularImage ivDoctorHead;

    public EaseChatRowSystemCard(Context context, EMMessage message, int position, BaseAdapter adapter,int state) {
        super(context, message, position, adapter,state);
    }

    @Override
    protected void onInflatView() {
        if(message.direct() == EMMessage.Direct.RECEIVE){
            inflater.inflate(R.layout.ease_row_received_message,this);
        }
    }

    @Override
    protected void onFindViewById() {
//        tvTitle = (TextView) findViewById(R.id.tv_title);
        tvSubTitle = (TextView) findViewById(R.id.tv_chatcontent);
        ivDoctorHead = (CircularImage) findViewById(R.id.iv_doctorhead);
    }

    @Override
    protected void onUpdateView() {
        adapter.notifyDataSetChanged();
    }

    @Override
    protected void onSetUpView() {
        EMTextMessageBody txtBody = (EMTextMessageBody) message.getBody();
        try {
            JSONObject object = new JSONObject("{"+txtBody.getMessage()+"}");
//            String title = (String) object.get("title");
            String content = (String) object.get("content");
//            tvTitle.setText(title);
            tvSubTitle.setText(content);
        } catch (JSONException e) {
            e.printStackTrace();
            return;
        }

        if(!message.isAcked() && message.getChatType() == EMMessage.ChatType.Chat){
            try {
                EMClient.getInstance().chatManager().ackMessageRead(message.getFrom(), message.getMsgId());
            } catch (HyphenateException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    protected void onBubbleClick() {

//        ActivityUtils.showActivity(activity, HealthyCurrencyActivity.class);
//        activity.finish();
//        String[] str = tvSubTitle.getText().toString().trim().split("：");
//        CopyDialog dialog = new CopyDialog(context,str[1].trim());
//        dialog.show();
    }
}
